const common = require('../../../utils/util')
const app = getApp()
// 获取全局数据对象
var globalData = app.globalData

Page({
  /**
   * 页面的初始数据
   */
  data: {
    requestURL: 'http://localhost:8081/keepfit/find_sport',
    dayStyle: [
      { month: 'current', day: new Date().getDate(), color: 'white', background: '#AAD4F5' },
      { month: 'current', day: new Date().getDate(), color: 'white', background: '#AAD4F5' }
    ],
    find_notice: '请按照下列条件查询（提示：全不选则查询所有)',
    isShowCalender: false,  // 是否显示日历插件  false:不显示   
    isChecked: false, //是否选中了复选框 false：代表没有
    items: [
      { name: '时间' },
      { name: '运动名称' },
      { name: '强度' },
      { name: '时间段' },
    ],
    isDisabled: true,

    isDisabled_0: true,
    isDisabled_1: true,// 是否对输入框禁用 true：禁用    
    isDisabled_2: true,
    isDisabled_3: true,
    calendar_img: '/images/diet/calendar.png',

    date: '',   // 日期（年月日）
    inputValue: null,
    DataList: [], // 用于获取服务器发过来的数据
  },
  //给点击的日期设置一个背景颜色  event事件里面当前含有点击的日期数据
  dayClick: function (event) {
    var that = this
    console.log(event)
    let clickDay = event.detail.day;
    let changeDay = `dayStyle[1].day`;
    let changeBg = `dayStyle[1].background`;
    this.setData({
      [changeDay]: clickDay,
      [changeBg]: "#84e7d0",
    })
    // 获取日期（年月日)
    var year = event.detail.year
    var month = event.detail.month
    var day = event.detail.day

    if(month>=10 && month<=12){
      month=month
    }else{
      month='0'+month
    }

    if(day>=10 && day<=12){
      day=day
    }else{
      day='0'+day
    }

    var date = year + "-" + month + "-" + day
    console.log('你点击的日期是:' + date)
    that.setData({
      date: date
    })
  },
  // 当点击日历图标时，显示日历
  HandleCalendar() {
    var that = this


    // 如果时间后面的框被勾选了的话，则将其日历进行显示
    if (!that.data.isDisabled_0) {
      // 点击日历图标时，日历消失
      if (that.data.isShowCalender) {
        that.setData({
          isShowCalender: false
        })
      } else {
        that.setData({
          isShowCalender: true
        })
      }

    } else {
      that.setData({
        isShowCalender: false  //不显示日历
      })
    }
  },
  onLoad: function () {

  },
  /**
   *  获取点击复选框时的value值，其value代表索引，以便找到对应的输入框
   * 
   */
  checkboxChange(e) {
    var that = this
    console.log('你点击了checkbox！！')
    console.log(e)
    var checkBoxValue = []
    checkBoxValue = e.detail.value

    // 如果第1个复选框被选中
    if (checkBoxValue.includes('0')) {
      that.setData({
        isDisabled_0: false
      })
    } else {
      that.setData({
        isDisabled_0: true,
        isShowCalender: false
      })
    }

    // 如果第2个复选框被选中
    if (checkBoxValue.includes('1')) {
      that.setData({
        isDisabled_1: false
      })
    } else {
      that.setData({
        isDisabled_1: true
      })
    }

    // 如果第3个复选框被选中
    if (checkBoxValue.includes('2')) {
      that.setData({
        isDisabled_2: false
      })
    } else {
      that.setData({
        isDisabled_2: true
      })
    }

    // 如果第4个复选框被选中
    if (checkBoxValue.includes('3')) {
      that.setData({
        isDisabled_3: false
      })
    } else {
      that.setData({
        isDisabled_3: true
      })
    }
  },
  HandleSubmit(e) {
    var that = this
    // 将表单中的数据进行清空
    this.setData({
      inputValue: null,
      isChecked: false, // 不勾选 复选框      
    })
    // 将表单设为禁止编辑状态
    that.DisableInput()
    console.log('下面为提交表单的数据')
    console.log(e)
    var sport_name = e.detail.value.sport_name
    var strength = e.detail.value.strength
    var timeSlot = e.detail.value.timeSlot

    //如果一个提交参数参数都没有的话
    // if(sport_name=='' && strength=='' && timeSlot=='' && this.data.date==''){
    //    wx.showToast({
    //      title: '至少选择一个!',
    //      icon:'error',
    //      duration:1000,
    //    })
    // }   
    var params = {}
    params = that.saveInputData(sport_name, strength, timeSlot, that.data.date)
    // 将日期设置为null
    this.setData({
      date: '',
    })
    console.log("参数：")
    for (var item in params) {
      console.log(item + "---" + params[item])
    }


    // 请求后台接口 上传参数
    common.findSportDetails(params, this.data.requestURL)
      .then(res => {
        that.setData({
          DataList: res
        })
        console.log('hello world---------------')
        if (res != null && res.length > 0) {
          wx.showToast({
            title: '查询成功!',
            icon: 'success',
            duration: 1000,
          })
          // 将查询到的数据存入全局对象globalData中 属性=dietDetails
          globalData.dietDetails = res
          // 将页面跳转到显示数据的页面上
          wx.navigateTo({
            url: '../find/find_show/index',
          })
        } else {
          wx.showToast({
            title: '查询失败!',
            icon: 'fail',
            duration: 1000,
          })
        }
      })
      .catch(err => {
        console.log(err)
      })

  },
  // 获取用户名
  getUsername() {
    var user = wx.getStorageSync('userMessage').user
    return user
  },
  // 表单重置事件
  HandleReset() {
    // 将表单全部设置为禁止编辑状态
    this.setData({
      isDisabled_0: true,
      isDisabled_1: true,
      isDisabled_2: true,
      isDisabled_3: true,
      date: '',
    })
  },
  // 将表单设为禁止编辑状态
  DisableInput() {
    this.setData({
      isDisabled_0: true,
      isDisabled_1: true,
      isDisabled_2: true,
      isDisabled_3: true,
    })

  },
  // 存储需要提交的表单参数
  saveInputData(sport_name, strength, timeSlot, date) {
    var params = {}  // 存储需要提交的表单参数
    if (sport_name != '') {
      params.sport_name = sport_name
    }
    if (strength != '') {
      params.strength = strength
    }
    if (timeSlot != '') {
      params.time_slot = timeSlot
    }
    // 时间设置
    if (date != '') {
      params.specific_time = date
    }

    if (this.getUsername() != null && this.getUsername() != '') {
      params.username = this.getUsername()
    } else {
      console.log('用户名为空null')
    }
    return params
  }
})